package net.davidlauzon.assemblandroid.views;

import net.davidlauzon.assemblandroid.R;
import net.davidlauzon.assemblandroid.TimeTrackerApplication;
import net.davidlauzon.assemblandroid.TimeTrackerModel.ACTIVITY;
import net.davidlauzon.assemblandroid.models.Ticket;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TicketDetailsActivity extends Activity {
	private static final String TAG = "AssemblaTT";
	
	// GRASP Controller
	private TimeTrackerApplication app;
	
	private TextView txtName;
	private TextView txtStatus;
	private TextView txtHours;
	private TextView txtDescription;

	private Ticket ticket;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "TicketDetailsActivity::onCreate()");
        
        // Data
        app = TimeTrackerApplication.getInstance();
        
        ticket = app.curTicket();
        
        
        // View
        setContentView(R.layout.ticket_details);
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.ticketDetailsLayout);
        //View layout = (View) findViewById(R.layout.ticket_details);
         
        txtName = (TextView) layout.findViewById(R.id.txtName);
        txtStatus = (TextView) layout.findViewById(R.id.txtStatus);
        txtHours = (TextView) layout.findViewById(R.id.txtHours);
        txtDescription = (TextView) layout.findViewById(R.id.txtDescription);
    }
    
    
    /**
     * Called just before the activity starts interacting with the user. 
     * 
     * At this point the activity is at the top of the activity stack, with user input going to it.
     * 
     * Always followed by onPause().
     */
    @Override
    public void onResume() {
    	super.onResume();
        Log.i(TAG, "TicketDetailsActivity::onResume()");
        
        txtName.setText(ticket.shortFormattedName());
        
        if (ticket.unassigned())
        	txtStatus.setText("Status: " + ticket.statusName() + "   (UNASSIGNED)");
        else
        	txtStatus.setText("Status: " + ticket.statusName());
        
        int percent = Math.round( 100 * ticket.workedHours() / ticket.workingHours() );
        txtHours.setText("Hours:  " 
        		+ ticket.workedHoursHuman() + " / " + ticket.workingHoursHuman() 
        		+ String.format("   (%d%%)", percent) );
        
        txtDescription.setText("Description: \n" + ticket.description());
        
        app.onActivityChanged(ACTIVITY.TICKET_DETAILS);
    }
    
    
    public void onStartClicked(View v) {
    	app.startTimeEntry();
    	
    	// Launch another activity
    	Intent myIntent = new Intent(this, TimeEntryActivity.class);
    	this.startActivity(myIntent);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "TicketDetailsActivity::onDestroy()");
    }
}
